<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="padding-left:20px">
      <el-breadcrumb-item :to="{path:'/2-1'}">老师管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/2-1'}">老师列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row class="content">
      <el-col :span="4" :offset="17" style="margin-top: 50px">
        添加老师
        <el-button type="primary" icon="el-icon-circle-plus" circle @click="toadd"></el-button>
      </el-col>
      <el-col :span="18" :offset="3" style="margin-top: 10px">
        <el-table border :data="tableData" v-loading="loading2">
          <el-table-column type="index" label="序号" class-name="index" width="70">
          </el-table-column>
          <el-table-column prop="name" label="老师名字" class-name="index">
          </el-table-column>
          <el-table-column prop="sex" label="性别" :formatter="sexFormat" class-name="index">
          </el-table-column>
          <el-table-column prop="createAt" label="创建时间" :formatter="dateFormat" width="180" class-name="index">
          </el-table-column>
          <el-table-column label="操作" width="400" class-name="index">
            <template slot-scope="scope">
              <el-button type="danger" icon="el-icon-delete" @click="delteacher(scope.row)">删除
              </el-button>
              <el-button type="primary" icon="el-icon-news" @click="tocheck(scope.row)">查看</el-button>
              <el-button type="warning" icon="el-icon-edit-outline" @click="toedit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="2" :offset="3" style="margin-top: 10px">
        <el-pagination
          :page-sizes="[10, 20, 30, 50]"
          :page-size="parseInt(pager.size)"
          layout="total, sizes, prev, pager, next, jumper"
          :total="parseInt(total)"
          @current-change="handleCurrentChange"
          :current-page="parseInt(pager.page)"
          @size-change="handleSizeChange">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import axios from 'axios'
  import {
    formatDate
  } from "../../../common/common";
  import {
    teacherlist, delteacher
  } from "@/api/teacher/teacher";
  export default {
    name: "TeachList",
    inject: ['reload'],
    data() {
      return {
        total:'',
        tableData: [],
        loading2: true,
        pager: {
          page: '',
          size: ''
        }
      }
    },
    methods: {
      dateFormat: function (row, column, cellValue) {
        var date = cellValue;
        if (date == undefined) {
          return "";
        }
        return this.$moment(cellValue).format("YYYY-MM-DD HH:mm:ss");
      },
      sexFormat: function (row, column, cellValue) {
        switch (cellValue) {
          case 1:
            return '男';
            break;
          case 2:
            return '女';
            break;
          case 0:
            return '胖鹏';
            break;
        }
      },
      delteacher(row) {
        console.log(row.id)
        this.$confirm('确定删除老师信息吗?',
          '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
          this.$message({
            type: 'success',
            message: '已删除老师信息'
          });
          let _this = this;
          delteacher(row.id).then(function (res) {
            console.log(res)
            _this.reload();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      tocheck(row) {
        this.$router.push({
          name: 'TeacherCheck',
          params: {
            index: row.id
          }
        })
      },
      toadd() {
        this.$router.push({
          name: 'TeacherAdd'
        })
      },
      toedit(row) {
        this.$router.push({
          name: 'TeacherEdit',
          params: {
            index: row.id
          }
        })
      },
      getDetailInfo() {
        let params = {};
        if (localStorage.tacpage) {
          this.pager.page = localStorage.tacpage;
        } else {
          this.pager.page = 1;
        }
        this.pager.size=localStorage.accsize?localStorage.accsize:10;
        params.page = this.pager.page;
        params.size = this.pager.size;
        params.types = 1;
        teacherlist(params).then(this.handleGetDataSucc)
      },
      handleGetDataSucc(res) {
        console.log(res);
        if (res) {
          this.loading2 = false;
        }
        this.tableData = res.data.UserList;
        this.total=parseInt(res.data.totle);
      },
      handleCurrentChange(val) {
        console.log(val);
        localStorage.tacpage = val;
        this.pager.page = val;
        this.getDetailInfo();
      },
      handleSizeChange(val) {
        this.pager.size = val;
        localStorage.accsize=val;
        this.getDetailInfo();
      },
    },
    mounted() {
      this.getDetailInfo();
    }
  }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";
  /deep/ .index {
    text-align: center;
  }
  .wrapper {
    .w100;
    .pager {
      margin: 20px;
    }
  }
</style>
